// pages/login/login.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    code: "",
    redirecturl: "",
    count: 0,
    form:{
      mobile:""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      redirecturl: options.redirecturl
    })
    this.getPage()
  },
  checkmobile(mobile) {

    let re = /^1[3,4,5,6,7,8,9][0-9]{9}$/;
    let result = re.test(mobile)
    console.log(result)
    if (!result) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: "none"
      })
      return false
    } else {
      return true
    }
  },
  async fsyzm() {
    console.log(this.data.mobile)
    if (this.checkmobile(this.data.mobile)) {
      console.log("手机号正确")
      const res = await wx.http({
        url: `/code?mobile=${this.data.mobile}`,
        method: "get",

      });
      console.log(res)
      this.setData({
        code: res.data.code,
         count: 60
      })


    } else {
      console.log("手机号不正确")
    }

  },
  countDownChange(e) {
    console.log(e)
    this.setData({
      count: e.detail.seconds
    })
  },
  async login() {
    if (this.data.mobile == '' || this.data.code == '') {
      wx.showToast({
        title: '请输入手机号和验证码',
        icon: "none"
      })
    } else {
      const res = await wx.http({
        url: "/login",
        method: "post",
        data: {
          mobile: this.data.mobile,
          code: this.data.code
        }

      })
      console.log(res)
      if (res.code == 10000) {
        wx.setStorageSync("token", 'Bearer ' + res.data.token);
        app.token = 'Bearer ' + res.data.token
        app.refreshToken = 'Bearer ' + res.data.refreshToken
        wx.setStorageSync('refreshToken','Bearer ' + res.data.refreshToken )
        console.log(this.data.redirecturl)
        wx.redirectTo({

          url: '/' + this.data.redirecturl,

        })
        // wx.redirectTo({
        //   url: '/pages/wdfw/wdfw',
        // })
      }

    }
  },
  getPage() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})